/* 
品牌街列表页逻辑
FDG416
2018/03/08
ID534 【商城需求】首页品牌街有点击更多的按钮（此按钮在设计中），点击更多，新增一页品牌街列表页
*/
$(function(){
    ajaxShow(0,1);
    /*二级导航栏逻辑*/ 
    $('.brand-nav > li').each(function(i,ele){
        $(this).click(function(){
            if($(this).find('span').hasClass('br-border-bottom')) {
                return;
            }
            $('#brand-dropload-div').html(' <ul class="clearfix brands-ul"></ul>');
            ajaxShow($(this).attr('data-attr'),1);
            $(this).find('span').addClass('br-border-bottom').siblings('i').addClass('brand-before-bgc');
            $(this).siblings('li').find('span').removeClass('br-border-bottom').siblings('i').removeClass('brand-before-bgc');
        })
        if($('.brand-nav > li').length > 6) {
            $('.brand-nav').addClass('hide').siblings('.nice-select').removeClass('hide');
        } 
    })
    /*下拉框*/ 
    $('[name="nice-select"]').click(function(e){
        $('[name="nice-select"]').find('ul').hide();
        $(this).find('ul').show();
        $(this).find('.brand-icon').addClass('brand-icon-up').removeClass('brand-icon-down');
        e.stopPropagation();
    });
    $('[name="nice-select"] li').hover(function(e){
        $(this).toggleClass('on');
        e.stopPropagation();
    });
    $('[name="nice-select"] li').click(function(e){
        if($(this).hasClass('selected')) {
            return;
        }
        var val = $(this).text();
        $('#brand-dropload-div').html(' <ul class="clearfix brands-ul"></ul>');
        ajaxShow($(this).attr('data-attr'),1);
        $(this).addClass('selected').siblings().removeClass('selected').parents('[name="nice-select"]').find('input').val(val);
        $('[name="nice-select"] ul').hide();
        $('[name="nice-select"]').find('.brand-icon').removeClass('brand-icon-up').addClass('brand-icon-down');
        e.stopPropagation();
    });
    $(document).click(function(){
        $('[name="nice-select"] ul').hide();
        $('[name="nice-select"]').find('.brand-icon').removeClass('brand-icon-up').addClass('brand-icon-down');
    });
})

function hoverStyle(){
    $('.brands-ul .brands-item').each(function(){
        var _that = $(this);
        //判断是否IE/火狐浏览器
        var _brands_item_p_w = $(this).find('p').text().length;
        _isIE();
        function _isIE() {
            var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
            var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1;
            var isH = userAgent.indexOf('Firefox') > -1;
            if(isIE || isH) {
                if(_brands_item_p_w > 28) {
                    $(_that).find('p').text($(_that).find('p').text().slice(0,28) + '...');
                }
            }
            if(isH) {
                $('.nice-select ul').css('top','28px');
            }
        }
        /*鼠标hover逻辑*/
        $(this).find('.br-a-img').hover(function(){
            $(_that).find('.logo-img').show();
        },function(){
            $(_that).find('.logo-img').hide();
        })
    })
}


//新闻动态列表页
function ajaxShow(brand_category_id,paging) {
    var brand_category_id = brand_category_id||0;
    var paging = paging||1;
    if(1==paging){
        $.ajax({
            type: 'POST',
            data:{'brand_category_id':brand_category_id,'page':paging},
            url: '/brand/get-brand-list-info',
            dataType: 'json',
            success: function(data){
                // 每次数据加载完，必须重置
                    var htmlInfo    = '';
                    $.each(data.data,function(i,datalist){
                        htmlInfo +='<li class="fl brands-item">';
                        if(datalist.bannerConfig[0]){
                            if(datalist.bannerConfig[0].product_url){
                                htmlInfo +='<a href="'+datalist.bannerConfig[0].product_url+'" class="pr br-a-img">';
                            }else{
                                htmlInfo +='<a href="'+url_to+'/brand_id/'+datalist.brand_id+'" class="pr br-a-img">';
                            }
                            htmlInfo +='<img src="'+image_bath_url+'/'+datalist.bannerConfig[0].brand_list_picture_url+'" title="'+datalist.bannerConfig[0].picture_name+'" alt="'+datalist.bannerConfig[0].picture_name+'" class="brand-img">';
                            htmlInfo +='<img src="'+image_bath_url+'/'+datalist.bannerConfig[0].brand_interactive_picture_url+'" title="'+datalist.bannerConfig[0].picture_name+'" alt="'+datalist.bannerConfig[0].picture_name+'" class="logo-img pa">';
                            htmlInfo +='</a>';
                            htmlInfo +='<p>'+datalist.bannerConfig[0].picture_name+'</p>';
                        }
                        htmlInfo +='</li>';
                    });
                    setTimeout(function () {
                        if(paging == 1){
                            $('.brands-ul').html(htmlInfo);
                        }else{
                            $('.brands-ul').append(htmlInfo);
                        }
                        /*鼠标hover逻辑 切换分类清空brands-ul 中的内容 TODO */
                        hoverStyle(); 
                       
                    }, 500);
                    
                   
                },
                error: function(xhr, type){
                    // 即使加载出错，也得重置
                    me.resetload();
                }
            });    
        }
    // dropload函数接口设置
    $('#brand-dropload-div').dropload({
        scrollArea: window,
        domDown: {
            domClass: 'dropload-down',
            // 滑动到底部显示内容
            domRefresh: '<div class="dropload-refresh"><span class="loading"></span></div>',
            // 内容加载过程中显示内容
            domLoad: '<div class="dropload-load"><span class="loading"></span></div>',
            // 没有更多内容-显示提示
            domNoData: '<div class="dropload-noData"></div>'
        },
        // 下拉刷新模块显示内容
        loadDownFn: function (me) {
            paging++; // 每次请求，页码加1
            getBrandAjax(me,brand_category_id,paging);
        },
         threshold:300
    });
}
function getBrandAjax(me,brand_category_id,paging){
    $.ajax({
        type: 'POST',
        data:{'brand_category_id':brand_category_id,'page':paging},
        url: '/brand/get-brand-list-info',
        dataType: 'json',
        success: function(data){
            // 每次数据加载完，必须重置
            if(200==data.code){
                if(data.data.length==0){
                    if(me != ''){
                        me.lock();
                        me.noData();
                        me.resetload();
                    }
                }else{
                    var htmlInfo    = '';
                    $.each(data.data,function(i,datalist){
                        htmlInfo +='<li class="fl brands-item">';
                        if(datalist.bannerConfig[0]){
                            if(datalist.bannerConfig[0].product_url){
                                htmlInfo +='<a href="'+datalist.bannerConfig[0].product_url+'" class="pr br-a-img">';
                            }else{
                                htmlInfo +='<a href="'+url_to+'/brand_id/'+datalist.brand_id+'" class="pr br-a-img">';
                            }
                            htmlInfo +='<img src="'+image_bath_url+'/'+datalist.bannerConfig[0].brand_list_picture_url+'" title="'+datalist.bannerConfig[0].picture_name+'" alt="'+datalist.bannerConfig[0].picture_name+'" class="brand-img">';
                            htmlInfo +='<img src="'+image_bath_url+'/'+datalist.bannerConfig[0].brand_interactive_picture_url+'" title="'+datalist.bannerConfig[0].picture_name+'" alt="'+datalist.bannerConfig[0].picture_name+'" class="logo-img pa">';
                            htmlInfo +='</a>';
                            htmlInfo +='<p>'+datalist.bannerConfig[0].picture_name+'</p>';
                        }
                        htmlInfo +='</li>';
                    });
                    setTimeout(function () {
                        if(paging == 1){
                            $('.brands-ul').html(htmlInfo);
                        }else{
                            $('.brands-ul').append(htmlInfo);
                        }
                        /*鼠标hover逻辑 切换分类清空brands-ul 中的内容 TODO */
                        hoverStyle(); 
                        if(me != ''){
                            me.resetload();
                        }
                    }, 500);
                }
            }
        },
        error: function(xhr, type){
            // 即使加载出错，也得重置
            me.resetload();
        }
    });
}